package com.kupurui.asteacher.ui.index;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.android.frame.util.Toolkit;
import com.android.frame.view.linearlistview.LinearListView;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.IndexClassAdapter;
import com.kupurui.asteacher.adapter.UriImageHolderView;
import com.kupurui.asteacher.bean.IndexClassInfo;
import com.kupurui.asteacher.bean.IndexInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseFgt;
import com.kupurui.asteacher.ui.WebLoadUrlAty;
import com.kupurui.asteacher.ui.mine.message.MessageAty;
import com.kupurui.asteacher.utils.UserManger;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/7/17.
 */

public class IndexFgt extends BaseFgt {

    @Bind(R.id.tv_rank)
    TextView tvRank;
    @Bind(R.id.tv_index_title)
    TextView tvIndexTitle;
    @Bind(R.id.tv_message)
    TextView tvMessage;
    @Bind(R.id.banner)
    ConvenientBanner banner;
    @Bind(R.id.tv_msg_count)
    TextView tvMsgCount;
    @Bind(R.id.rl_msg_countFrame)
    RelativeLayout rlMsgCountFrame;
    @Bind(R.id.imgv_right)
    ImageView imgvRight;
    @Bind(R.id.relatively_student_examine)
    RelativeLayout relativelyStudentExamine;
    @Bind(R.id.tv_class_num)
    TextView tvClassNum;
    @Bind(R.id.relatively_my_class)
    RelativeLayout relativelyMyClass;
    @Bind(R.id.listview)
    LinearListView listview;


    IndexInfo indexInfo;
    List<IndexInfo.ClassListBean> list;
    IndexClassAdapter adapter;


    private List<String> bannerList;
    private List<IndexInfo.BannerBean> imgs;

    @Override
    public int getLayoutId() {
        return R.layout.index_fgt;
    }

    @Override
    public void initData() {
        tvIndexTitle.setText(UserManger.getUserInfo().getName()+"老师，欢迎您");

        bannerList = new ArrayList<>();
        imgs = new ArrayList<>();
        list = new ArrayList<>();
        adapter = new IndexClassAdapter(getActivity(),list,R.layout.index_class_item);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new LinearListView.OnItemClickListener() {
            @Override
            public void onItemClick(LinearListView parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putString("class_id",list.get(position).getId());
                startActivity(ClassDetailsAty.class,bundle);
            }
        });
    }

    @Override
    public void requestData() {
        showLoadingContentDialog();
        new Teacher().xueshengIndex(UserManger.getId(),this,0);
    }

    @Override
    public void onResume() {
        super.onResume();
        new Teacher().xueshengIndex(UserManger.getId(),this,0);
    }

    @OnClick({R.id.relatively_student_examine,R.id.tv_rank,R.id.tv_message})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.relatively_student_examine:
                startActivity(BindingAuditAty.class,null);
                break;
            case R.id.tv_rank://排行榜
                showToast("该功能正在建设中。。。");
                break;
            case R.id.tv_message://消息
                startActivity(MessageAty.class,null);
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                indexInfo = AppJsonUtil.getObject(result,IndexInfo.class);
                if (indexInfo.getShenhe_num().equals("0")){
                    rlMsgCountFrame.setVisibility(View.GONE);
                }else{
                    rlMsgCountFrame.setVisibility(View.VISIBLE);
                    tvMsgCount.setText(indexInfo.getShenhe_num());
                }
                tvClassNum.setText("共"+indexInfo.getBanji_num()+"个");
                imgs.clear();
                imgs.addAll(indexInfo.getBanner());
                list.clear();
                list.addAll(indexInfo.getClass_list());
                adapter.notifyDataSetChanged();
                bannerList.clear();
                for (int i = 0;i < imgs.size();i++){
                    bannerList.add(imgs.get(i).getBanner());
                }
                setBanner();
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    private void setBanner() {
        if (Toolkit.listIsEmpty(imgs))
            return;

        banner.setPages(new CBViewHolderCreator<UriImageHolderView>() {
            @Override
            public UriImageHolderView createHolder() {
                return new UriImageHolderView();
            }
        }, bannerList).setPageIndicator(new int[]{R.drawable.shape_circle_gray, R.drawable.shape_circle_blue}, 0)
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
                .setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {

                    }
                });
        banner.startTurning(8000);

        banner.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                //点击事件
                Bundle bundle = new Bundle();
                bundle.putString("url",imgs.get(position).getUrl());
                startActivity(WebLoadUrlAty.class,bundle);
            }
        });
    }
}
